package cn.lycode.generator.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.lycode.common.core.domain.AjaxResult;
import cn.lycode.common.core.page.PageResult;
import cn.lycode.generator.domian.dto.DbTableListDTO;
import cn.lycode.generator.domian.dto.GeneTableDTO;
import cn.lycode.generator.domian.dto.ImportTableDTO;
import cn.lycode.generator.domian.dto.SelectTablesDTO;
import cn.lycode.generator.domian.entity.GeneratorTable;
import cn.lycode.generator.domian.vo.GenCheckedInfoVO;
import cn.lycode.generator.domian.vo.GeneratorDetailVO;
import cn.lycode.generator.domian.vo.GeneratorPreviewVO;
import cn.lycode.generator.service.GeneratorTableService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * TODO: 代码生成器重构，更佳灵活简洁
 * <p>
 * 代码生成 控制器
 * </p>
 */
@RestController
@RequestMapping("/generator")
@RequiredArgsConstructor
public class GeneratorTableController {

    private final GeneratorTableService generatorTableService;

    /**
     * 查询要导入的表列表（数据库中的表，排除已经导入的表）
     */
    @SaCheckPermission(value = "generator:table:list")
    @GetMapping("/schema/list")
    public AjaxResult<PageResult<GeneratorTable>> schemaList(DbTableListDTO dto) {
        return AjaxResult.success(generatorTableService.selectDbTableNotInImport(dto));
    }
    /**
     * 导入指定表
     */
    @SaCheckPermission(value = "generator:table:import")
    @PostMapping("/import")
    public AjaxResult<Object> importTables(@RequestBody ImportTableDTO dto) {
        generatorTableService.importTable(dto);
        return AjaxResult.success();
    }

    /**
     * 查询已经导入的表列表
     */
    @SaCheckPermission(value = "generator:table:list")
    @GetMapping("/list")
    public AjaxResult<PageResult<GeneratorTable>> list(DbTableListDTO dto) {
        return AjaxResult.success(generatorTableService.selectDbTableByImport(dto));
    }

    //删除导入的表
    @SaCheckPermission(value = "generator:table:remove")
    @PostMapping("/delete")
    public AjaxResult<Object> delete(@RequestBody SelectTablesDTO dto) {
        generatorTableService.delete(dto);
        return AjaxResult.success();
    }

    /**
     * 代码生成参数详情
     */
    @GetMapping("/{tableName}")
    public AjaxResult<GeneratorDetailVO> detail(@PathVariable String tableName) {
        return AjaxResult.success(generatorTableService.detail(tableName));
    }

    //更新代码生成配置
    @SaCheckPermission(value = "generator:config:update")
    @PostMapping("/update")
    public AjaxResult<Object> updateGenerator(@RequestBody GeneratorDetailVO dto) {
        generatorTableService.updateGeneratorSetting(dto);
        return AjaxResult.success();
    }

    /**
     * 预览代码
     */
    @SaCheckPermission(value = "generator:code:preview")
    @GetMapping("/preview/{tableName}")
    public AjaxResult<List<GeneratorPreviewVO>> preview(@PathVariable String tableName) throws IOException {
        return AjaxResult.success(generatorTableService.preview(tableName));
    }

    /**
     * zip下载
     */
    @SaCheckPermission(value = "generator:code:zip")
    @PostMapping("/zip")
    public void downloadZip(@RequestBody SelectTablesDTO dto, HttpServletResponse response) throws IOException {
        byte[] data = generatorTableService.downloadZip(dto);
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Content-Disposition", "attachment; filename=\"sz-admin.zip\"");
        response.setContentLength(data.length);

        // 将字节数组写入输出流
        IOUtils.write(data, response.getOutputStream());
        response.flushBuffer();
    }


    /**
     * 验证磁盘
     */
    @SaCheckPermission(value = "generator:table:check")
    @GetMapping("/check/{tableName}")
    public AjaxResult<GenCheckedInfoVO> check(@PathVariable String tableName) {
        return AjaxResult.success(generatorTableService.checkDist(tableName));
    }

    //代码生成
    @SaCheckPermission(value = "generator:code:generator")
    @PostMapping("/generator")
    public AjaxResult<List<String>> generator(@RequestBody GeneTableDTO dto) throws IOException {
        return AjaxResult.success(generatorTableService.generator(dto));
    }

    //同步表
    @SaCheckPermission(value = "generator:table:remove")
    @PostMapping("/sync")
    public AjaxResult<Object> sync(@RequestBody ImportTableDTO dto) {
        generatorTableService.sync(dto);
        return AjaxResult.success();
    }




}
